package HRMPROJECT.VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.JDateChooser;

import HRMPROJECT.CONTROLLERS.DepartmentController;
import HRMPROJECT.CONTROLLERS.SearchControllers;
import HRMPROJECT.MODELS.SearchingEntity;
import HRMPROJECT.MODELS.SecurityContexts;

public class frmSearchingInfo extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtEmpInfo;
    private final JTable jtbKetQua;
    private JComboBox cboPhongBan = null;
    private final JDateChooser dtpTuNgay;
    private final JDateChooser dtpDenNgay;

    private Connection m_objConnection = null;
    private static Connection objConn;
    private static SecurityContexts objSec;
    private SecurityContexts m_objSecurity = null;
    private DepartmentController m_objDepController = null;
    private DefaultTableModel m_objDtm = null;
    private SearchingEntity objSeachingInfo;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frmSearchingInfo frame = new frmSearchingInfo(objConn, objSec);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frmSearchingInfo(Connection objConnection, SecurityContexts objSecurity) {
        m_objConnection = objConnection;
        objConn = m_objConnection;
        m_objSecurity = objSecurity;
        objSec = m_objSecurity;

        m_objDtm = new DefaultTableModel();
        objSeachingInfo = new SearchingEntity();
        m_objDepController = new DepartmentController(m_objConnection, m_objSecurity);

        setIconImage(Toolkit.getDefaultToolkit().getImage(frmSearchingInfo.class.getResource("/HRMPROJECT/IMAGES/search.png")));
        setTitle("TÌM KIẾM THÔNG TIN");
        setResizable(false);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 852, 466);
        contentPane = new JPanel();
        contentPane.setBorder(new TitledBorder(null, "T\u00ECm Ki\u1EBFm Th\u00F4ng Tin", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
                "T\u00ECm th\u00F4ng tin Nh\u00E2n Vi\u00EAn", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel.setBounds(10, 22, 299, 87);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Nhập ký tự bất kỳ cần tìm kiếm:");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(10, 22, 279, 18);
        panel.add(lblNewLabel);

        txtEmpInfo = new JTextField();
        txtEmpInfo.setBackground(Color.ORANGE);
        txtEmpInfo.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtEmpInfo.setBounds(10, 44, 279, 31);
        panel.add(txtEmpInfo);
        txtEmpInfo.setColumns(10);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
                "T\u00ECm th\u00F4ng tin Ch\u1EA5m C\u00F4ng c\u1EE7a Nh\u00E2n Vi\u00EAn", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        panel_1.setBounds(318, 22, 508, 87);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        JLabel lblNewLabel_1 = new JLabel("Chọn Phòng Ban:");
        lblNewLabel_1.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel_1.setBounds(10, 23, 112, 14);
        panel_1.add(lblNewLabel_1);

        cboPhongBan = new JComboBox();
        cboPhongBan.addItem("    ");
        cboPhongBan.setBounds(117, 21, 381, 20);
        panel_1.add(cboPhongBan);

        JLabel lblTNgy = new JLabel("Từ Ngày :");
        lblTNgy.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblTNgy.setBounds(20, 59, 112, 14);
        panel_1.add(lblTNgy);

        dtpTuNgay = new JDateChooser();
        dtpTuNgay.setDateFormatString("yyyy-MM-dd");
        dtpTuNgay.setBounds(117, 56, 153, 20);
        panel_1.add(dtpTuNgay);

        JLabel lbln = new JLabel("đến :");
        lbln.setFont(new Font("Cambria", Font.PLAIN, 14));
        lbln.setBounds(280, 59, 38, 14);
        panel_1.add(lbln);

        dtpDenNgay = new JDateChooser();
        dtpDenNgay.setDateFormatString("yyyy-MM-dd");
        dtpDenNgay.setBounds(315, 56, 183, 20);
        panel_1.add(dtpDenNgay);

        JButton btnNewButton = new JButton("Đóng");
        btnNewButton.setBackground(new Color(0, 191, 255));
        btnNewButton.setIcon(new ImageIcon(frmSearchingInfo.class.getResource("/HRMPROJECT/IMAGES/deleteIcon.png")));
        btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnNewButton.setBounds(707, 120, 119, 27);
        contentPane.add(btnNewButton);

        JButton btnTmKim = new JButton("Tìm Kiếm");
        btnTmKim.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bFlag = false;
                if (objSeachingInfo == null) {
                    objSeachingInfo = new SearchingEntity();
                }
                if (txtEmpInfo.getText().isEmpty()) {
                    objSeachingInfo.setEmpInfo(null);
                } else {
                    bFlag = true;
                    objSeachingInfo.setEmpInfo(txtEmpInfo.getText());
                }
                int iPhongBan = cboPhongBan.getSelectedIndex();
                if (iPhongBan == 0) {
                    objSeachingInfo.setDepartId(null);
                } else {
                    bFlag = true;
                    int DepartId = m_objDepController.getSelectedValue(cboPhongBan, cboPhongBan.getSelectedIndex());
                    objSeachingInfo.setDepartId("" + DepartId);
                }
                String sTuNgay = ((JTextField) dtpTuNgay.getDateEditor().getUiComponent()).getText();
                String sDenNgay = ((JTextField) dtpDenNgay.getDateEditor().getUiComponent()).getText();
               
                if (sTuNgay.isEmpty() && sDenNgay.isEmpty()) {
                    objSeachingInfo.setStartDayOf(null);
                    objSeachingInfo.setEndDayOf(null);
                } else {
                    bFlag = true;
                    objSeachingInfo.setStartDayOf(sTuNgay);
                    objSeachingInfo.setEndDayOf(sDenNgay);
                }

                // 2.
                if (bFlag) {
                    SearchControllers objController = new SearchControllers(m_objConnection, m_objSecurity);
                    try {
                        m_objDtm = objController.SearchTableModels(objSeachingInfo);
                        jtbKetQua.setModel(m_objDtm);

                    } catch (SQLException e1) {
                        System.out.println(e1.getMessage());
                    }
                } else {
                    JOptionPane.showMessageDialog(getParent(), "Vui lòng chọn từ khóa cần tìm kiếm!");
                }
            }
        });
        btnTmKim.setBackground(new Color(0, 191, 255));
        btnTmKim.setIcon(new ImageIcon(frmSearchingInfo.class.getResource("/HRMPROJECT/IMAGES/search.png")));
        btnTmKim.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnTmKim.setBounds(583, 120, 119, 27);
        contentPane.add(btnTmKim);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
                "K\u1EBFt Qu\u1EA3 T\u00ECm Ki\u1EBFm", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0,
                        0)));
        panel_2.setBounds(10, 158, 826, 269);
        contentPane.add(panel_2);
        panel_2.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 13, 806, 247);
        panel_2.add(scrollPane);

        jtbKetQua = new JTable();
        scrollPane.setViewportView(jtbKetQua);

        // Hiển thị phòng ban lên comboBox
        try {
            m_objDepController.getDepartmentOf(cboPhongBan);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

}
